package leecode

import "math"

func findRestaurant(list1 []string, list2 []string) (result []string) {
	hashList1 := make(map[string]int) //key为字符串 , value为对应的下标
	for i := 0; i < len(list1); i++ {
		hashList1[list1[i]] = i
	}
	hashMap := make(map[string]int) //key为共同出现的字符串 , value为索引和
	for i := 0; i < len(list2); i++ {
		if index1, ok := hashList1[list2[i]]; ok {
			//两人的共同餐厅
			indexSum := index1 + i
			hashMap[list2[i]] = indexSum
		}
	}
	//寻找最小的索引和
	minIndex := math.MaxInt
	for _, index := range hashMap {
		minIndex = min(index, minIndex)
	}
	//根据最小索引和寻找符合的餐厅
	for rest, index := range hashMap {
		if index == minIndex {
			result = append(result, rest)
		}
	}
	return
}
